package com.demo.core.dao;

import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.demo.base.model.PartTimeJob;
import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;

/**
 * Created by jiandandia on 2017/4/4.
 */
@Repository
public class PartTimeJobDao extends BaseDao {

    public PartTimeJob newOrupdatePartTimeJob(PartTimeJob partTimeJob) throws Exception {
        return saveEntity(partTimeJob);
    }

    public PartTimeJob getPartTimeJobById(String partTimeJobId) throws Exception {
        PartTimeJob retval = null;
        try {
            if (!StringUtils.isEmpty(partTimeJobId)) {
                retval = (PartTimeJob) this.get(PartTimeJob.class, partTimeJobId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
        return retval;
    }

    public IPagableDTO<PartTimeJob> getPartTimeJobByQuery(PartTimeJob partTimeJob, IPagableDTO pagableDTO) {
        IPagableDTO<PartTimeJob> retval = new PagableDTO<PartTimeJob>();
        try {
            Session session = getSession();
            Criteria c = session.createCriteria(PartTimeJob.class);
            //兼职类型
            if(null != partTimeJob.getType()){
                c.add(Restrictions.eq("type", partTimeJob.getType()));
            }
            //工作性质
            if(null != partTimeJob.getJobNature()){
                c.add(Restrictions.eq("jobNature", partTimeJob.getJobNature()));
            }
            //工作类型
            if(null != partTimeJob.getJobNature()){
                c.add(Restrictions.eq("workType", partTimeJob.getWorkType()));
            }
            //工作区域
            if(null != partTimeJob.getJobNature()){
                c.add(Restrictions.eq("workArea", partTimeJob.getWorkArea()));
            }
            //工作环境
            if(null != partTimeJob.getJobNature()){
                c.add(Restrictions.eq("workEnvironment", partTimeJob.getWorkEnvironment()));
            }

            //后期加其他查询条件
            Long totalRow = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
            retval.setTotalRow(totalRow.intValue());
            c.setProjection(null);
            if (null != pagableDTO) {
                c.setFirstResult(pagableDTO.getStartRow());
                c.setFetchSize(pagableDTO.getPageSize());
                c.setMaxResults(pagableDTO.getPageSize());
            }
            retval.setData(c.list());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retval;
    }

    public void deltePartTimeJobById(String id) throws Exception{
        Session s = getSession();
        try {
            s.delete(s.get(PartTimeJob.class, id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    public boolean addBrowsingNumber(String noticeId) {
        try {
            String hql = "update PartTimeJob set browsingNumber=browsingNumber + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{noticeId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean addFollowersNumber(String noticeId) {
        try {
            String hql = "update PartTimeJob set followersNumber=followersNumber + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{noticeId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean addManEnterNumber(String id) {
        try {
            String hql = "update PartTimeJob set manEnter=manEnter + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{id});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean addWoManEnterNumber(String id) {
        try {
            String hql = "update PartTimeJob set womanEnter=womanEnter + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{id});
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}